$(document).ready(function() {
    init();
});

function init(){
    town_values();
    
    $('#towns').change(function(){
        ballot_values($(this).val());
    });
}

function town_values() {
    findTowns(createAddOptionHandler($('#towns')));
}

function ballot_values(townId) {
    findBallotsPerTown(townId, createAddOptionHandler($('#ballots')));
}

function findTowns(callback, error_callback) {
    $.ajax({
        url:envAppHome + '/election-counting/towns',
        contentType:'application/json',
        dataType:'json',
        type:'POST',
        success:callback,
        error:error_stub(error_callback)
    });
}

function findBallotsPerTown(townId, callback, error_callback) {
    if(townId) {
        $.ajax({
            url:envAppHome + '/election-counting/ballot-town/?townId=' + townId,
            contentType:'application/json',
            dataType:'json',
            type:'POST',
            success:callback,
            error:error_stub(error_callback)
        });
    }
}